/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webanovare.model;

import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 *
 * @author daniel
 */
public class UserList {
    
    private static UserList instance;

    private SortedMap<Date, String> userTree = new TreeMap<Date, String>();
    
    private UserList() {
    }
    public static UserList getInstance() {
        if (UserList.instance == null) {
            UserList.instance = new UserList();
        }
        return UserList.instance;
    }
    public void addUser(String userName, Date validUntil) {
        userTree.put(validUntil, userName);
        
        // Remove all old entries
        userTree = userTree.tailMap(new Date(System.currentTimeMillis()));
    }
    public List<String> getUsers() {
        LinkedList<String> apa = new LinkedList<> ();
        apa.addAll(userTree.values());
        Collections.sort(apa);
        
        String prevUserName = "";
        for (int i = 0; i < apa.size(); i++) {
            if (apa.get(i).equals(prevUserName)) {
                apa.remove(i);
                i--;
            }
            else {
                prevUserName = apa.get(i);
            }
        }
        
        return apa;
    }
}
